package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb01Zh;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb01ZhExc;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.DictionaryBean;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb01ZhService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 字典表1综合
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "字典表1综合")
@RestController
@RequestMapping("/dictionary/cb01Zh")
@Validated
public class Cb01ZhController {

    @Resource
    private ICb01ZhService iCb01ZhService;

    /**
     * 添加字典表1综合
     */
    @ApiOperation("添加字典表1综合")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb01Zh cb01Zh) {
        iCb01ZhService.save(cb01Zh);
        return CommonResult.ok();
    }

    /**
     * 删除字典表1综合
     */
    @ApiOperation("删除字典表1综合")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb01ZhService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑字典表1综合
     */
    @ApiOperation("编辑字典表1综合")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb01Zh cb01Zh) {
        iCb01ZhService.updateById(cb01Zh);
        return CommonResult.ok();
    }

    /**
     * 字典表1综合分页查询
     */
    @ApiOperation("字典表1综合分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb01Zh>> page() {
        return CommonResult.data(iCb01ZhService.page(new Cb01Zh()));
    }

    /**
     * 矿产组合下拉框
     */
    @ApiOperation("矿产组合下拉框")
    @GetMapping("/mineralsZh")
    public CommonResult<List<Cb01Zh>> mineralsZh() {
        return CommonResult.data(iCb01ZhService.mineralsZh());
    }

    /**
     * 矿产字典查询
     */
//    @ApiOperation("矿产字典查询")
//    @GetMapping("/getDictionaryBean")
//    public CommonResult<DictionaryBean> getDictionaryBean() {
//        return CommonResult.data(iCb01ZhService.getDictionaryBean());
//    }

    @ApiOperation("矿产字典查询-重构")
    @GetMapping("/getDictionaryBean")
    public CommonResult<DictionaryBean> getKcDictionary() {
        return CommonResult.data(iCb01ZhService.getKcDictionary());
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb01ZhExc> list = ExcelImportUtil.readMultipartFile(file, Cb01ZhExc.class);
            List<Cb01Zh> cb01Zhs = GsonUtil.ObjectToList(list, Cb01Zh.class);
            iCb01ZhService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
